package com.itheima.health.service.impl;

import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConst;
import com.itheima.health.common.TypeConstant;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.service.ValidateCodeService;
import com.itheima.health.utils.AliyunSmsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class ValidateCodeServiceImpl implements ValidateCodeService {
    @Autowired
    private AliyunSmsUtil aliyunSmsUtil;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 发送验证码
     * @param type
     * @param telephone
     */
    @Override
    public void validateCode(String type, String telephone) {
        try {
            String code = aliyunSmsUtil.sendSms(telephone);
            log.info(code);
            if (type.equals(TypeConstant.LOGIN_TYPE)){
                redisTemplate.opsForValue().set(RedisConst.LOGIN_VALIDATE_CODE_PREFIX+telephone,code);
            }else{
                redisTemplate.opsForValue().set(RedisConst.ORDER_VALIDATE_CODE_PREFIX+telephone, code);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessRuntimeException(MessageConst.SEND_VALIDATECODE_FAIL);
        }
    }
}
